package com.crpc.runner;

import cn.hutool.extra.spring.SpringUtil;
import com.crpc.common.crpcEnum.CrpcExceptionEnums;
import com.crpc.common.exception.CrpcException;
import com.crpc.core.CrpcBootStrap;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

import static com.crpc.core.CrpcBootStrap.SPRINGBOOt_SERVICE_Configs;

@Component
public class NettServiceRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        //启动netty服务器
        CompletableFuture.runAsync(() -> {
            try {
                CrpcBootStrap.getInstance()
                        .publish(SPRINGBOOt_SERVICE_Configs)
                        .start();
            } catch (Exception e) {
                e.printStackTrace();
                throw new CrpcException(CrpcExceptionEnums.NETTY_ERROR);
            }
        }, SpringUtil.getBean("CrpcTaskExecutor"));
    }

}
